package com.booking.utils;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Build.VERSION_CODES;
import android.preference.PreferenceManager;

/**
 * @author iholub Jun 4, 2013
 */
public final class PreferenceHelper {

	private static enum ConfigTypes {
		SAVED_LATITUDE, SAVED_LONGITUDE, SAVED_ZOOM, PREF_CACHE_CLEANING_TIMESTAMP
	}

	@TargetApi(VERSION_CODES.GINGERBREAD)
	public static void setSavedLatitude(Context context, double latitude) {
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(context);
		Editor editor = prefs.edit();
		editor.putLong(ConfigTypes.SAVED_LATITUDE.name(),
				Double.doubleToLongBits(latitude));
		if (VersionUtlis.hasGingerbread()) {
			editor.apply();
		} else {
			editor.commit();
		}
	}

	public static double getSavedLatitude(Context context) {
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(context);
		return Double.longBitsToDouble(prefs.getLong(
				ConfigTypes.SAVED_LATITUDE.name(), 0));
	}

	@TargetApi(VERSION_CODES.GINGERBREAD)
	public static void setSavedLongitude(Context context, double longitude) {
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(context);
		Editor editor = prefs.edit();
		editor.putLong(ConfigTypes.SAVED_LONGITUDE.name(),
				Double.doubleToLongBits(longitude));
		if (VersionUtlis.hasGingerbread()) {
			editor.apply();
		} else {
			editor.commit();
		}
	}

	public static double getSavedLongitude(Context context) {
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(context);
		return Double.longBitsToDouble(prefs.getLong(
				ConfigTypes.SAVED_LONGITUDE.name(), 0));
	}

	@TargetApi(VERSION_CODES.GINGERBREAD)
	public static void setSavedZoom(Context context, float zoom) {
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(context);
		Editor editor = prefs.edit();
		editor.putFloat(ConfigTypes.SAVED_ZOOM.name(), zoom);
		if (VersionUtlis.hasGingerbread()) {
			editor.apply();
		} else {
			editor.commit();
		}
	}

	public static float getSavedZoom(Context context) {
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(context);
		return prefs.getFloat(ConfigTypes.SAVED_ZOOM.name(), 0);
	}

	@TargetApi(VERSION_CODES.GINGERBREAD)
	public static void setCacheClearingTime(Context context, long time) {
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(context);
		Editor editor = prefs.edit();
		editor.putLong(ConfigTypes.PREF_CACHE_CLEANING_TIMESTAMP.name(), time);
		editor.apply();
	}

	public static long getCacheClearingTime(Context context) {
		SharedPreferences prefs = PreferenceManager
				.getDefaultSharedPreferences(context);
		return prefs.getLong(ConfigTypes.PREF_CACHE_CLEANING_TIMESTAMP.name(),
				0);
	}
}
